package com.todod.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestAttribute;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.todod.entity.ResultData;
import com.todod.mapper.ShuoMingCatalogMapper;
import com.todod.model.ShuoMingCatalog;
import com.todod.model.ShuoMingFile;
import com.todod.service.ShuoMingCatalogService;
import com.todod.service.ShuoMingFileService;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

/**
 * @author lql
 * @date 2020年12月25日
 */
@RestController
@RequestMapping("/shuoMingCatalog")
public class ShuoMingCatalogController extends BaseManageController {

	@Autowired
	public ShuoMingCatalogService shuoMingCatalogService;
	@Autowired
	public ShuoMingCatalogMapper shuoMingCatalogMapper;
	@Autowired
	private ShuoMingFileService shuoMingFileService;

	/**
	 * 
	 * @param _userId
	 * @param request
	 * @param queryShuoMingFile
	 * @return
	 */
	@RequestMapping("/getTreeList")
	public ResultData getTreeList(@RequestAttribute Long _userId, Long xiangmuId) {

		// 获取个人创建数据
		List<ShuoMingCatalog> list = shuoMingCatalogService.getListByXmId(xiangmuId);

		JSONObject jo = new JSONObject();
		JSONArray ja = JSONArray.fromObject(jo);
		ja.remove(0);

		jo.put("id", xiangmuId);
		jo.put("name", "api目录");
		jo.put("parentId", "-1");
		jo.put("xmId", xiangmuId);
		jo.put("sortNum", "0");
		jo.put("flag", "ML");
		ja.add(jo);

		for (ShuoMingCatalog shuoMingCatalog1 : list) {
			jo.put("id", shuoMingCatalog1.getId());
			jo.put("name", shuoMingCatalog1.getName());
			jo.put("parentId", String.valueOf(shuoMingCatalog1.getParentId()));
			jo.put("xmId", shuoMingCatalog1.getXmId());
			jo.put("sortNum", shuoMingCatalog1.getSortNum());
			jo.put("flag", "ML");
			ja.add(jo);

			// 获取个人创建数据
			List<ShuoMingFile> plist = shuoMingFileService.getListByClassifyId(shuoMingCatalog1.getId());
			for (ShuoMingFile shuoMingFile : plist) {
				jo.put("id", shuoMingFile.getId());
				jo.put("name", shuoMingFile.getTableName());
				jo.put("parentId", String.valueOf(shuoMingFile.getClassifyId()));
				jo.put("xmId", shuoMingFile.getXmId());
				jo.put("sortNum", shuoMingFile.getSortNum());
				jo.put("flag", "WD");
				jo.put("code", shuoMingFile.getCode());
				ja.add(jo);
			}
		}

		return ResultData.success("ok", ja);
	}

	/**
	 * 
	 * @param _userId
	 * @param reqShuoMingCatalog
	 * @return
	 */
	@PostMapping("/save")
	public ResultData saveData(@RequestAttribute Long _userId, @RequestBody ShuoMingCatalog shuoMingCatalog) {

		shuoMingCatalogService.saveData(_userId, shuoMingCatalog);
		return ResultData.success();
	}

	/**
	 * 
	 * @param _userId
	 * @param reqShuoMingCatalog
	 * @return
	 */
	@PostMapping("/edit")
	public ResultData editData(@RequestAttribute Long _userId, @RequestBody ShuoMingCatalog shuoMingCatalog) {

		shuoMingCatalogService.editData(_userId, shuoMingCatalog);
		return ResultData.success();
	}

	/**
	 * 
	 * @param ids
	 * @return
	 */
	@RequestMapping("/delete")
	public ResultData delete(Long code) {
		if (code == null) {
			return ResultData.error(ResultData.PARAM_ERROR_CODE, "参数错误");
		}

		List<ShuoMingFile> list = shuoMingFileService.getListByXftId(code);

		if (list != null && list.size() > 0) {
			return ResultData.error(ResultData.PARAM_ERROR_CODE, "当前目录下已有文档，请先删除文档再删除目录");
		}

		shuoMingCatalogMapper.deleteById(code);
		return ResultData.success();
	}

	/**
	 * 
	 * @param code
	 * @return
	 */
	@GetMapping("/getInfo")
	public ResultData getInfo(Long code) {
		if (code == null) {
			return ResultData.error(ResultData.PARAM_ERROR_CODE, "参数错误");
		}
		return ResultData.success("ok", shuoMingCatalogService.getInfo(code));
	}

	/**
	 * @Title: getLeafNode
	 * @Description: 获取全部叶子节点
	 * @author dyl
	 * @date: 2021-01-28
	 * @return
	 * @return Result
	 */
	@GetMapping("/getAllLeafNode")
	public ResultData getAllLeafNode(Long xiangmuId) {

		// 获取个人创建数据
		List<ShuoMingCatalog> list = shuoMingCatalogService.getListByXmId(xiangmuId);
		ShuoMingCatalog shuoMingCatalog = new ShuoMingCatalog();
		shuoMingCatalog.setId(xiangmuId);
		shuoMingCatalog.setParentId(-1L);
		shuoMingCatalog.setSortNum(0);
		shuoMingCatalog.setName("api目录");
		shuoMingCatalog.setXmId(xiangmuId);

		list.add(shuoMingCatalog);

		return ResultData.success("ok", list);
	}

	/**
	 * 
	 * @param _userId
	 * @return
	 */
	@RequestMapping("/getInitialLoading")
	public ResultData getInitialLoading(@RequestAttribute Long _userId, Long xmId) {
		// 获取权限数据
		List<ShuoMingCatalog> list = shuoMingCatalogService.getListByUserIdAndType(_userId, xmId);
		return ResultData.success("ok", list);
	}

	/**
	 * 
	 * @param _userId
	 * @return
	 */
	@RequestMapping("/getAgainLoading")
	public ResultData getAgainLoading(@RequestAttribute Long _userId, Long xmId, Long parentId) {
		// 获取权限数据
		List<ShuoMingCatalog> list = shuoMingCatalogService.getListByUserIdAndTypeAndParentId(_userId, xmId, parentId);
		return ResultData.success("ok", list);
	}

}
